package cn.felord.code;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;

import java.io.*;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * The type Test.
 *
 * @author Dax
 * @since 11 :47  2018/8/22
 */
public class Test implements Serializable {
    /**
     * The entry point of application.
     *
     * @param args the input arguments
     * @throws IOException       the io exception
     * @throws TemplateException the template exception
     */
    public static void main(String[] args) throws IOException, TemplateException {

        String rootPath = "C:\\IdeaProjects\\codegenerator\\src\\main\\java";
        String packageName = "cn.felord.code";
        String templatePath = "C:\\IdeaProjects\\codegenerator\\src\\main\\resources\\templates";
        String templateName = "entity.ftl";


        JavaProperties userEntity = new JavaProperties("UserEntity", packageName);

        userEntity.addField(String.class, "username");
        userEntity.addField(LocalDate.class, "birthday");
        userEntity.addField(LocalDateTime.class, "addTime");
        userEntity.addField(Integer.class, "gender");
        userEntity.addField(Integer.class, "age");


        autoCodingJavaEntity(rootPath, templatePath, templateName, userEntity);

    }


    /**
     * Auto coding.
     *
     * @param rootPath       the root path
     * @param templatePath   the template path
     * @param templateName   the template name
     * @param javaProperties the java properties
     * @throws IOException       the io exception
     * @throws TemplateException the template exception
     */
    public static void autoCodingJavaEntity(String rootPath,
                                            String templatePath,
                                            String templateName,
                                            JavaProperties javaProperties) throws IOException, TemplateException {

        // freemarker 配置
        Configuration configuration = new Configuration(Configuration.DEFAULT_INCOMPATIBLE_IMPROVEMENTS);

        configuration.setDefaultEncoding("UTF-8");
        // 指定模板的路径
        configuration.setDirectoryForTemplateLoading(new File(templatePath));
        // 根据模板名称获取路径下的模板
        Template template = configuration.getTemplate(templateName);
        // 处理路径问题
        final String ext = ".java";
        String javaName = javaProperties.getEntityName().concat(ext);
        String packageName = javaProperties.getPkg();

        String out = rootPath.concat(Stream.of(packageName.split("\\."))
                .collect(Collectors.joining("/", "/", "/" + javaName)));

        // 定义一个输出流来导出代码文件
        OutputStreamWriter outputStreamWriter = new OutputStreamWriter(new FileOutputStream(out));
        // freemarker 引擎将动态数据绑定的模板并导出为文件
        template.process(javaProperties, outputStreamWriter);

    }


}
